package io.renren.modules.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.dtos.ResponseResult;
import io.renren.common.enums.AppHttpCodeEnum;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.hotel.dto.CommonQuestionMapper;
import io.renren.modules.hotel.entity.QuestionEntity;
import io.renren.modules.hotel.service.CommonQuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class CommonQuestionServiceImpl extends ServiceImpl<CommonQuestionMapper, QuestionEntity> implements CommonQuestionService {
    @Autowired
    CommonQuestionService commonQuestionService;
    @Autowired
    CommonQuestionMapper commonQuestionMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionEntity> page = this.page(new Query<QuestionEntity>()
                .getPage(params), new QueryWrapper<QuestionEntity>());

        return new PageUtils(page);
    }

    @Override
    public ResponseResult allCommonQuestion(QuestionEntity entity) {
        LambdaQueryWrapper<QuestionEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(entity.getCode())) {
            wrapper.eq(QuestionEntity::getCode, entity.getCode());
        }
        if (entity.getId() != 0) {
            wrapper.eq(QuestionEntity::getId, entity.getId());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public void insertOrupdate(QuestionEntity entity) {
        //新增
        if (entity.getId() == null) {
            QuestionEntity entity1 = new QuestionEntity();
            entity1.setCode(entity.getCode());
            entity1.setTitle(entity.getTitle());
            entity1.setContent(entity.getContent());
            save(entity1);
        }

        //修改
        QuestionEntity updateQuestion = commonQuestionMapper.selectById(Wrappers
                .<QuestionEntity>lambdaQuery().eq(QuestionEntity::getId, entity.getId()));
        updateQuestion.setContent(entity.getContent());
        updateById(entity);

    }


    @Override
    public void delQuestion(Long id) {
        if (id == null) {
            return;
        }
        commonQuestionService.removeById(id);
    }


}
